package com.reading.core.manager.impl;

import com.reading.common.entity.view.BookLike;
import com.reading.common.persistence.view.BookLikeMapper;
import com.reading.core.exception.AloneException;
import com.reading.core.exception.other.LikeBookFailException;
import com.reading.core.manager.ILikeManager;
import com.reading.core.utils.CheckUtil;
import com.reading.core.utils.GlobalConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author guwenqi_1995@163.com
 * @date 2019/7/9 3:05 PM
 **/
@Slf4j
@Service
public class LikeManagerImpl implements ILikeManager {

    @Resource
    private BookLikeMapper bookLikeMapper;

    @Override
    public void like(Integer bookId, Integer userId) throws AloneException {
        CheckUtil.isNull(bookId, userId);

        int row;
        BookLike like = new BookLike(bookId, userId);
        if (bookLikeMapper.selectBookAndUserLikeStatus(bookId, userId)
                == GlobalConstant.NUMBER_1) {
            row = bookLikeMapper.delete(like);
        } else {
            row = bookLikeMapper.insert(like);
        }

        if (row <= GlobalConstant.NUMBER_0) {
            log.error("点赞失败。{}, {}", bookId, userId);
            throw new LikeBookFailException();
        }
    }
}
